﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbLinksDal
	{
		public DbLinksDal()
		{
		}
 		#region Insert
 		public static int Insert(DbLinks dbLinks)
		{	
			string sql = @"Insert Into DbLinks (
					LType,
					LLogo,
					LTitle,
					LUrl,
					LViews,
					LAddTime,
					LFrom
				) values({0},'{1}','{2}','{3}',{4},'{5}',{6})";
			sql = string.Format(sql,
					dbLinks.LType,
					SqlHelper.String(dbLinks.LLogo),
					SqlHelper.String(dbLinks.LTitle),
					SqlHelper.String(dbLinks.LUrl),
					dbLinks.LViews,
					dbLinks.LAddTime,
					dbLinks.LFrom
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(DbLinks dbLinks)
		{	
			string sql = @"Update DbLinks set 

					LType= {0},
					LLogo= '{1}',
					LTitle= '{2}',
					LUrl= '{3}',
					LViews= {4},
					LAddTime= '{5}',
					LFrom= {6}
				where

					LId= {7}";
			sql = string.Format(sql,
					dbLinks.LType,
					SqlHelper.String(dbLinks.LLogo),
					SqlHelper.String(dbLinks.LTitle),
					SqlHelper.String(dbLinks.LUrl),
					dbLinks.LViews,
					dbLinks.LAddTime,
					dbLinks.LFrom,
					dbLinks.LId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 lId)
		{
		
			string sql = @"Delete from DbLinks where 
					LId= {0}";
			sql = string.Format(sql,
					lId
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from DbLinks a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbLinks Select(Int32 lId)
		{
			DbLinks dbLinks = null;
			string sql = @"select a.* from DbLinks a  where
					a.LId= {0}
";
			sql = string.Format(sql,
					lId
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbLinks = new DbLinks(ds.Tables[0].Rows[0]);					
			return dbLinks;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from DbLinks a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbLinks a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from DbLinks a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
